<?php

namespace app\admin\model;

use app\admin\validate\CheckArticleCate;
use app\common\impl\Comm;
use app\common\model\AdminBaseModel;
use think\Exception;
use think\facade\Db;
use Org\Tree;
use think\facade\Route;


class ArticleCates extends AdminBaseModel implements Comm
{
    //表名
    protected $name = "articles_cate";

    public function getAdminPageData($param = [], $order = 'id asc')
    {
        // TODO: Implement getAdminPageData() method.

        $where = [];
        if (isset($param['name']) && $param['name'] != '') {
            $where[] = ['name', 'like', "%" . $param['name'] . "%"];
        }


        if (isset($param['time']) && $param['time'] != '') {
            $ck = @explode(" ~ ", $param['time']);
            $b = $ck[0] . " 00:00:00";
            $e = $ck[1] . " 23:59:59";
            $where[] = ['create_time', 'between', [strtotime($b), strtotime($e)]];
        }
        if (!empty($param['is_show'])) {
            $status = (int)$param['is_show'] - 1;
            $where[] = ['is_show', '=', $status];
        }

        try {
            $data = self::limit_select($where, $order);
            $count = self::hhy_count($where);

            if (!empty($data)) {
                $data = Tree::toFormatTree($data, "name");
            }

            return parent::ajaxResult($data, $count);
        } catch (Exception $exception) {
            return parent::ajaxResult([], 0, 100, $exception->getMessage());
        }
    }


    public function delById($id)
    {
        // TODO: Implement delById() method.

        return parent::del($id);
    }


    public function doAll($data)
    {
        // TODO: Implement doAll() method.
        try {
            $check = new CheckArticleCate();
            if (!$check->check($data)) {
                return parent::JsonReturn($check->getError(), 0);
            }


            if (isset($param['id'])) {
                $res = self::update($data);
                $id = $data['id'];
            } else {
                $res = self::save($data);
                $id = $this->id;
            }

            if ($res) {
                return self::JsonReturn("更新添加成功");
            } else {
                return self::JsonReturn("更新添加失败", 0);
            }
        } catch (\Exception $e) {
            return self::JsonReturn($e->getMessage(), 0);
        }
    }




    public function edit($param)
    {
        return self::update($param);
    }




}